<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'" width="530px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
      <el-form-item label="计划名称" >
        <el-input v-model="form.planName" style="width: 370px;"/>
      </el-form-item>
      <!-- <el-form-item label="巡检对象id" >
        <el-input v-model="form.objId" style="width: 370px;"/>
      </el-form-item> -->
      <!-- <el-form-item label="巡检对象" >
        <el-input v-model="form.objName" style="width: 370px;"/>
      </el-form-item> -->
      <el-form-item label="巡检对象" >
       <el-select v-model="form.objId" placeholder="巡检对象" v-on:change="objectSelect($event)">
           <el-option v-for="(itemobject,index) in inspectionObject" :key="index" :label="itemobject.label" :value="itemobject.id">
           </el-option>
      </el-select>
      </el-form-item>
      <!-- <el-form-item label="巡检工具id" >
        <el-input v-model="form.toolId" style="width: 370px;"/>
      </el-form-item> -->
     <el-form-item label="巡检模板" >
       <el-select v-model="form.templateId" placeholder="巡检模板" >
           <el-option v-for="(itemobject,index) in inspectionTemplate" :key="index" :label="itemobject.templateName" :value="itemobject.id">
           </el-option>
      </el-select>
      </el-form-item>
       <!-- <el-form-item label="巡检模板" >
        <el-input v-model="form.templateName" style="width: 370px;"/>
      </el-form-item> -->
      <el-form-item label="巡检工具" >
       <el-select v-model="form.toolId" placeholder="巡检工具" v-on:change="objectSelect($event)">
           <el-option v-for="(itemobject,index) in toolItem" :key="index" :label="itemobject.label" :value="itemobject.id">
           </el-option>
      </el-select>
      </el-form-item>
      <!-- <el-form-item label="巡检工具" >
        <el-input v-model="form.toolName" style="width: 370px;"/>
      </el-form-item> -->
      <!-- <el-form-item label="巡检模板id" >
        <el-input v-model="form.templateId" style="width: 370px;"/>
      </el-form-item> -->
 
      <el-form-item label="巡检计划说明" >
        <el-input v-model="form.planDesc" style="width: 370px;"/>
      </el-form-item>
      <!-- <el-form-item label="巡检周期id" >
        <el-input v-model="form.periodId" style="width: 370px;"/>
      </el-form-item> -->

      <!-- <el-form-item label="巡检人id" >
        <el-input v-model="form.inspectorId" style="width: 370px;"/>
      </el-form-item> -->
      <el-form-item label="服务商" >
       <el-select v-model="form.inspectorId" placeholder="服务商" v-on:change="indexSelect($event)">
           <el-option v-for="(item,index) in serviceproItem" :key="index" :label="item.seproName" :value="item.id">
           </el-option>
      </el-select>
      </el-form-item>
      <!-- <el-form-item label="巡检人" >
        <el-input v-model="form.inspectorName" style="width: 370px;"/>
      </el-form-item> -->
      <!-- <el-form-item label="巡检车辆类型id" >
        <el-input v-model="form.vehtypeId" style="width: 370px;"/>
      </el-form-item> -->
      <el-form-item label="巡检车辆" >
       <el-select v-model="form.vehTypeId" placeholder="巡检车辆" v-on:change="objectSelect($event)">
           <el-option v-for="(itemobject,index) in vehtypeItem" :key="index" :label="itemobject.label" :value="itemobject.id">
           </el-option>
      </el-select>
      </el-form-item>
      <!-- <el-form-item label="巡检车辆" >
        <el-input v-model="form.vehtypeName" style="width: 370px;"/>
      </el-form-item> -->
      <el-form-item label="巡检周期" >
       <el-select v-model="form.periodId" placeholder="巡检周期" v-on:change="objectSelect($event)">
           <el-option v-for="(itemobject,index) in periodItem" :key="index" :label="itemobject.name" :value="itemobject.id">
           </el-option>
      </el-select>
      </el-form-item>



       <!-- <el-form-item label="巡检周期" >
        <el-input v-model="form.periodName" style="width: 370px;"/>
      </el-form-item> -->
      <el-form-item label="开始时间" >
        <el-date-picker
          style="width: 230px"
          type="datetime"
          placeholder="请选择"
          v-model="form.startTime"
          @change="startTimeChange"
          :format="'yyyy-MM-dd HH:mm:ss'"
          :picker-options="startTimeConfig">
      </el-date-picker>
        <!-- <el-input v-model="form.startTime" style="width: 370px;"/> -->
      </el-form-item>
      <el-form-item label="结束时间" >
        <el-date-picker
          style="width: 230px"
          type="datetime"
          placeholder="请选择"
          v-model="form.endTime"
          @change="endTimeChange"
          :format="'yyyy-MM-dd HH:mm:ss'"
          :picker-options="endTimeConfig">
      </el-date-picker>
        <!-- <el-input v-model="form.endTime" style="width: 370px;"/> -->
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit } from '@/api/inspectionPlan'
import request from '@/utils/request'
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false, dialog: false,
      form: {
        id: '',
        planName: '',
        objId: '',
        objName: '',
        toolId: '',
        toolName: '',
        templateId: '',
        templateName: '',
        planDesc: '',
        periodId: '',
        periodName: '',
        inspectorId: '',
        inspectorName: '',
        vehtypeId: '',
        vehtypeName: '',
        startTime: '',
        endTime: ''
      },
       inspectionObject:[],
       inspectionTemplate:[],
       serviceproItem:[],
       periodItem:[
        {
            id:1,
            name:'日'
        },{
            id:2,
            name:'周',
        },{
            id:3,
            name:'月'
        },{
            id:4,
            name:'季'
        },
        // {
        //     id:5,
        //     name:'半年'
        // },
        {
            id:6,
            name:'年'
        }
         ],
      toolItem:[],
      vehtypeItem:[],
       //-----------------
      realTimeInfo: {
          startTime: '',
          endTime: ''
         },
         startTimeConfig: {},
         endTimeConfig: {},
       //---------------
      rules: {
      }
    }
  },
   mounted () {
        this.initTimeConfig();
        this.tServiceproviderInfoAll();
        this.byNameinfo("巡检工具");
        this.byNameVehtypeinfo("巡检车辆");
        //获取所有的巡检对象

        //获取所有的巡检末班
        
    },
  methods: {
     tServiceproviderInfoAll(){
        this.getAllService().then(res => {
          this.serviceproItem=res;
        //根据id再进行全部查询
      })
    },
    getAllService() {
      return request({
        url: 'api/tServiceproviderInfoAll',
        method: 'get'
      })
    },
    //日期S
        initTimeConfig () {
          let self = this
          // 开始时间配置
          this.startTimeConfig = {
              disabledDate (time) {
                  if (self.realTimeInfo.endTime) {
                      return time.getTime() > new 
                      Date(self.realTimeInfo.endTime).getTime()
                  }
              }
          },
          // 结束时间配置
          this.endTimeConfig = {
              disabledDate (time) {
                  if (self.realTimeInfo.startTime) {
                      return time.getTime() < new 
                      Date(self.realTimeInfo.startTime).getTime()
                  }
              }
          }
      },
      startTimeChange (val) {
          this.realTimeInfo.startTime = val
      },
      endTimeChange (val) {
          this.realTimeInfo.endTime = val
      },
    //--------------------------
      objectSelect(event){
        debugger;
        this.getTembyObjectId(event).then(res => {
        this.inspectionTemplate=res;
      })
      },
                  // 获取id值
    getTembyObjectId(id) {
      return request({
        url: 'api/inspectionTemplate/byObjectId/' + id,
        method: 'get'
      })
    },
    //--------------------------
    byNameinfo(name) {
          this.byName(name).then(res => {
            this.bydictId(res.id).then(resobject => {
              this.toolItem=resobject;
            //根据id再进行全部查询
          })
            //根据id再进行全部查询
          })
        },
     byNameVehtypeinfo(name) {
          this.byName(name).then(res => {
            this.bydictId(res.id).then(resobject => {
              this.vehtypeItem=resobject;
            //根据id再进行全部查询
          })
            //根据id再进行全部查询
          })
        },
            // 获取id值
        byName(name) {
          return request({
            url: 'api/dict/byName/' + name,
            method: 'get'
          })
        },
            // 获取id值
        bydictId(id) {
          return request({
            url: 'api/dictDetail/findByDeptId/' + id,
            method: 'get'
          })
        },

    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.loading = true
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        planName: '',
        objId: '',
        objName: '',
        toolId: '',
        toolName: '',
        templateId: '',
        templateName: '',
        planDesc: '',
        periodId: '',
        periodName: '',
        inspectorId: '',
        inspectorName: '',
        vehtypeId: '',
        vehtypeName: '',
        startTime: '',
        endTime: ''
      }
    }
  }
}
</script>

<style scoped>

</style>
